package com.starblink.message.messagelikeorcomment.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.util.post.PostUtils
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CommUtils
import com.starblink.message.Constants
import com.starblink.message.databinding.ItemMessageSystemBinding
import com.starblink.rocketreserver.FetchUserMessagesV2Query


class MessageSystemAdapter :
    BaseQuickAdapter<FetchUserMessagesV2Query.FetchUserMessagesV2, MessageSystemAdapter.VH>() {

    class VH(var binding: ItemMessageSystemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        val binding = ItemMessageSystemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return VH(binding)
    }

    override fun onBindViewHolder(
        holder: VH,
        position: Int,
        item: FetchUserMessagesV2Query.FetchUserMessagesV2?,
    ) {
        item?.apply {
            holder.binding.tvTime.text = PostUtils.getTimeDiffDetailFriendly(sendTime as String)

            if (talentAccount?.auditStatus == Constants.AuditStatus.REJECT
                || talentAccount?.auditStatus == Constants.AuditStatus.PASS
            ) {
                // 驳回消息或通过消息
                holder.binding.ivPostMainPic.visibility = View.GONE
                (holder.binding.tvContent.layoutParams as ViewGroup.MarginLayoutParams).topMargin =
                    CommUtils.dip2px(10)
                holder.binding.tvContent.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray600))
                val title: String
                val content: String
                if (talentAccount?.auditStatus == Constants.AuditStatus.REJECT) {
                    title = "Creator Access Application Rejected"
                    content =
                        "Sorry your creator access application has not been approved at this time. ${item.talentAccount?.auditMsg}"
                } else {
                    title = "Creator Access Application Approved"
                    content =
                        "Congratulations, your application for creator access  has been approved."
                }
                holder.binding.tvTitle.text = title
                holder.binding.tvContent.text = content

                if (!jumpUrl.isNullOrEmpty()) {
                    holder.binding.root.setOnClickListener {
                        navigationTo(RoutePage.Web.PAGE_GLOBAL_H5) {
                            withString(RoutePage.Web.ARG_URL, jumpUrl!!)
                        }
                    }
                } else {
                    holder.binding.root.setOnClickListener(null)
                }
            } else {
                holder.binding.ivPostMainPic.visibility = View.VISIBLE
                (holder.binding.tvContent.layoutParams as ViewGroup.MarginLayoutParams).topMargin =
                    CommUtils.dip2px(4)
                holder.binding.tvContent.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray400))

                ImageUtils.loadImage(
                    context,
                    post?.postF?.mainPic,
                    holder.binding.ivPostMainPic,
                    ImageUtils.errorImage2,
                    ImageUtils.errorImage2
                )
                holder.binding.tvTitle.text = "The content has since been deleted"
                holder.binding.tvContent.text = post?.postF?.content ?: ""
                holder.binding.root.setOnClickListener(null)
            }
        }
    }
}